import 'package:flutter/material.dart';
import 'package:flutter_app_test/test1.dart';

main()=>runApp(MyApp());


class MyApp extends StatelessWidget {
   MyApp({Key? key}) : super(key: key);

  ValueChanged<int> valueChanged=(value){
      print("value $value");
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Aniamtion3Widget(valueChanged),);
  }
}


//初步使用
class Animation1 extends StatefulWidget {

  @override
  _Animation1State createState() => _Animation1State();
}

class _Animation1State extends State<Animation1>
    with SingleTickerProviderStateMixin {

 late Animation _animationTween;
 late AnimationController mController;
  @override
  void initState() {
    super.initState();
    mController =AnimationController(duration:Duration(seconds:3),vsync: this);
    var curvedAnimation = CurvedAnimation(parent: mController, curve: Curves.bounceInOut);
    _animationTween =Tween<double>(begin: 50.0,end: 300.0).animate(curvedAnimation)
    ..addStatusListener((status) {

    })
     ..addListener(() {
       setState(() {
       });
   });
    mController.forward();
  }

  @override
  void dispose() {
    super.dispose();
    mController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:Image.asset("res/drawable/drawable-xhdpi/img_personal_head.png",
         width:_animationTween.value,
         height: _animationTween.value,
      ),
    );
  }
}

////简单封装使用
class Aniamtion2 extends AnimatedWidget{

  Aniamtion2({Key? key,required Animation<double> animation})
      : super( key:key, listenable:animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable as Animation<double>;
   return Center(
     child: Image.asset("res/drawable/drawable-xhdpi/img_personal_head.png",
        width:animation.value,
        height: animation.value,
      ),
   );
  }
}

class Aniamtion2Widget extends StatefulWidget {
  @override
  _Aniamtion2WidgetState createState() => _Aniamtion2WidgetState();
}

class _Aniamtion2WidgetState extends State<Aniamtion2Widget> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> tween;
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this,duration: Duration(seconds: 3));
    var curveTween = CurvedAnimation(parent:_controller,curve: Curves.linear);
    tween = Tween(begin: 10.0,end: 300.0).animate(curveTween);
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Aniamtion2(animation: tween,);
  }
}


//
class Aniamtion3Widget extends StatefulWidget {

  ValueChanged<int> valueChanged;

  Aniamtion3Widget(this.valueChanged);

  @override
  _Aniamtion3WidgetState createState() => _Aniamtion3WidgetState();
}

class _Aniamtion3WidgetState extends State<Aniamtion3Widget> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> tween;
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this,duration: Duration(seconds: 3));
    var curveTween = CurvedAnimation(parent:_controller,curve: Curves.linear);
    tween = Tween(begin: 10.0,end: 300.0).animate(curveTween)..addStatusListener((status) {
      if(status==AnimationStatus.forward){
        print("forward");
      }else if(status==AnimationStatus.reverse){
        print("reverse");
      }else if(status==AnimationStatus.completed){
        print("completed");
        _controller.reverse();
      }else if(status==AnimationStatus.dismissed){
        print("dismissed");
        // _controller.forward();
      }

    })..addListener(() {
      widget.valueChanged.call(tween.value.toInt());
    });
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(animation: tween,
        child: Image.asset("res/drawable/drawable-xhdpi/img_personal_head.png",
        ),
        builder: (BuildContext context, Widget? child){
      return Center(
        child: Container(
          width:tween.value,
          height: tween.value,
          child: child,
        ),
      );
    });
  }
}